package com.jn.algorithm.day023.question71;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author 江南大学1033190417
 * @date 2022/5/14 10:06
 */
public class Solution {
    public String simplifyPath(String path) {
        String[] split = path.split("/");
        Deque<String> p = new ArrayDeque<>();
        for (String s : split) {
            if ("..".equals(s)) {
                if (!p.isEmpty()) {
                    p.pollLast();
                }
            } else if (s.length() > 0 && !".".equals(s)) {
                p.addLast(s);
            }
        }
        StringBuilder res = new StringBuilder();
        res.append(p.isEmpty() ? "/" : "");
        while (!p.isEmpty()) {
            res.append("/").append(p.poll());
        }
        return res.toString();
    }
}
